/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { RouteRecordName, RouteRecordRaw } from 'vue-router'
import type { VabRouteRecord } from '/#/router'
import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from 'vue-router'
import Layout from '@vab/layouts/index.vue'
import { setupPermissions } from './permissions'
import { authentication, isHashRouterMode, publicPath } from '@/config'

export const constantRoutes: VabRouteRecord[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/callback',
    name: 'Callback',
    component: () => import('@/views/callback/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue'),
    meta: {
      hidden: true,
    },
  },
  // {
  //   path: '/trackingList',
  //   name: 'TrackingList',
  //   component: () => import('@/views/customer/tracking/tracklist/index.vue'),
  //   meta: {
  //     title: '跟踪记录',
  //   },
  // },
]

export const asyncRoutes: VabRouteRecord[] = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index/index.vue'),
        meta: {
          title: '首页工作台',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/index/dashboard.vue'),
        meta: {
          title: '数据统计看板',
          icon: 'bar-chart-box-line',
        },
      },
      // {
      //   path: 'remixIcon',
      //   name: 'RemixIcon',
      //   component: () => import('@/views/vab/icon/remixIcon.vue'),
      //   meta: {
      //     title: '图标采纳',
      //     icon: 'image-2-line',
      //   },
      // },
    ],
  },
  // {
  //   path: '/systemset',
  //   name: 'SystemSet',
  //   component: Layout,
  //   meta: {
  //     title: '设置',
  //     icon: 'home-2-line',
  //     breadcrumbHidden: true,
  //   },
  // },
  {
    path: '/organization',
    name: 'Organization',
    component: Layout,
    meta: {
      title: '组织',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'organizationIndex',
        name: 'OrganizationIndex',
        component: () => import('@/views/organization/index.vue'),
        meta: {
          title: '组织管理',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/marketing',
    name: 'Marketing',
    component: Layout,
    meta: {
      title: '营销',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'marketingIndex',
        name: 'MarketingIndex',
        component: () => import('@/views/marketing/index.vue'),
        meta: {
          title: '营销管理',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'brand',
        name: 'Brand',
        component: () => import('@/views/marketing/brand/index.vue'),
        meta: {
          title: '品牌',
          icon: 'bar-chart-box-line',
        },
        children: [
          {
            path: 'brandDesign',
            name: 'BrandDesign',
            component: () => import('@/views/marketing/brand/design/index.vue'),
            meta: {
              title: '设计',
              icon: 'bar-chart-box-line',
            },
          },
        ],
      },
      {
        path: 'website',
        name: 'WebSite',
        component: () => import('@/views/marketing/website/index.vue'),
        meta: {
          title: '网站',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'selfmedia',
        name: 'SelfMedia',
        component: () => import('@/views/marketing/selfmedia/index.vue'),
        meta: {
          title: '自媒体',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'adverts',
        name: 'Adverts',
        component: () => import('@/views/marketing/adverts/index.vue'),
        meta: {
          title: '广告',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'media',
        name: 'Media',
        component: () => import('@/views/marketing/media/index.vue'),
        meta: {
          title: '媒体',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'activities',
        name: 'Activities',
        component: () => import('@/views/marketing/activities/index.vue'),
        meta: {
          title: '活动',
          icon: 'bar-chart-box-line',
        },
      },
    ],
  },
  {
    path: '/operation',
    name: 'Operation',
    component: Layout,
    meta: {
      title: '运营',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'operationIndex',
        name: 'operationIndex',
        component: () => import('@/views/operation/index.vue'),
        meta: {
          title: '运营',
          icon: 'bar-chart-box-line',
        },
      },
      // {
      //   path: 'publicOperation',
      //   name: 'PublicOperation',
      //   component: () => import('@/views/operation/public/index.vue'),
      //   meta: {
      //     title: '公域运营',
      //     icon: 'bar-chart-box-line',
      //   },
      // },
      // {
      //   path: 'selfOperation',
      //   name: 'SelfOperation',
      //   component: () => import('@/views/operation/self/index.vue'),
      //   meta: {
      //     title: '私域运营',
      //     icon: 'bar-chart-box-line',
      //   },
      // },
    ],
  },
  {
    path: '/customer',
    name: 'Customer',
    component: Layout,
    meta: {
      title: '客户',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'customerIndex',
        name: 'CustomerIndex',
        component: () => import('@/views/customer/index.vue'),
        meta: {
          title: '客户管理',
          icon: 'bar-chart-box-line',
        },
      },
    ],
  },
  {
    path: '/business',
    name: 'Business',
    component: Layout,
    meta: {
      title: '业务',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'businessIndex',
        name: 'BusinessIndex',
        component: () => import('@/views/business/index.vue'),
        meta: {
          title: '业务',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'productsIndex',
        name: 'ProductsIndex',
        component: () => import('@/views/business/products/index.vue'),
        meta: {
          title: '产品',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'ordersIndex',
        name: 'OrdersIndex',
        component: () => import('@/views/business/orders/index.vue'),
        meta: {
          title: '订单',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'cardsIndex',
        name: 'CardsIndex',
        component: () => import('@/views/business/cards/index.vue'),
        meta: {
          title: '卡',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'integralIndex',
        name: 'IntegralIndex',
        component: () => import('@/views/business/integral/index.vue'),
        meta: {
          title: '积分',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'exchangeIndex',
        name: 'ExchangeIndex',
        component: () => import('@/views/business/exchange/index.vue'),
        meta: {
          title: '兑换',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/opportunity',
    name: 'Opportunity',
    component: Layout,
    meta: {
      title: '机会',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'opportunityIndex',
        name: 'OpportunityIndex',
        component: () => import('@/views/opportunity/index.vue'),
        meta: {
          title: '机会',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'dynamic',
        name: 'Dynamic',
        component: () => import('@/views/opportunity/dynamic/index.vue'),
        meta: {
          title: '动态',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'news',
        name: 'News',
        component: () => import('@/views/opportunity/news/index.vue'),
        meta: {
          title: '新闻',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/financeTaxation',
    name: 'FinanceTaxation',
    component: Layout,
    meta: {
      title: '财税',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'financeTaxationIndex',
        name: 'FinanceTaxationIndex',
        component: () => import('@/views/financetaxation/index.vue'),
        meta: {
          title: '财税',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'financeIndex',
        name: 'FinanceIndex',
        component: () => import('@/views/financetaxation/finance/index.vue'),
        meta: {
          title: '财务',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'taxationIndex',
        name: 'TaxationIndex',
        component: () => import('@/views/financetaxation/taxation/index.vue'),
        meta: {
          title: '税务',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'assetsIndex',
        name: 'AssetsIndex',
        component: () => import('@/views/financetaxation/assets/index.vue'),
        meta: {
          title: '资产',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'manageFinance',
        name: 'ManageFinance',
        component: () =>
          import('@/views/financetaxation/managefinance/index.vue'),
        meta: {
          title: '理财',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'investmentIndex',
        name: 'InvestmentIndex',
        component: () => import('@/views/financetaxation/investment/index.vue'),
        meta: {
          title: '投资',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/chat',
    name: 'Chat',
    component: Layout,
    meta: {
      title: '沟通',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'chatIndex',
        name: 'ChatIndex',
        component: () => import('@/views/chat/index.vue'),
        meta: {
          title: '沟通',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/apps',
    name: 'Apps',
    component: Layout,
    meta: {
      title: '应用',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'appIndex',
        name: 'AppIndex',
        component: () => import('@/views/apps/index.vue'),
        meta: {
          title: '应用首页',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'appClass',
        name: 'AppClass',
        component: () => import('@/views/apps/class/index.vue'),
        meta: {
          title: '应用分类',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'applist',
        name: 'AppList',
        component: () => import('@/views/apps/app/index.vue'),
        meta: {
          title: '应用',
          icon: 'bar-chart-box-line',
        },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'NotFound',
    meta: {
      hidden: true,
    },
  },
]

const router = createRouter({
  history: isHashRouterMode
    ? createWebHashHistory(publicPath)
    : createWebHistory(publicPath),
  routes: constantRoutes as RouteRecordRaw[],
})

function fatteningRoutes(routes: VabRouteRecord[]): VabRouteRecord[] {
  return routes.flatMap((route: VabRouteRecord) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

function addRouter(routes: VabRouteRecord[]) {
  routes.forEach((route: VabRouteRecord) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export function resetRouter(routes: VabRouteRecord[] = constantRoutes) {
  routes.map((route: VabRouteRecord) => {
    if (route.children) route.children = fatteningRoutes(route.children)
  })
  router.getRoutes().forEach(({ name }) => {
    router.hasRoute(<RouteRecordName>name) &&
      router.removeRoute(<RouteRecordName>name)
  })
  addRouter(routes)
}

export function setupRouter(app: any) {
  if (authentication === 'intelligence') addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)
  return router
}

export default router
